package cn.nexd.app.collector.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import cn.nexd.app.collector.R;
import cn.nexd.app.collector.adapter.FileManagerAdapter;
import cn.nexd.app.collector.presenter.fileManager.FileManagerContract;
import cn.nexd.app.collector.presenter.fileManager.FileManagerPresenter;
import cn.nexd.app.collector.util.UMengUtil;
import cn.nexd.app.collector.widget.WarningDialog;
import cn.nexd.app.collector.widget.slidingtablayout.SlidingTabLayout;

/**
 * Created by wangxu on 16/10/8.
 */

public class FileManagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener, FileManagerContract.FileManagerActivityView, WarningDialog.OnDialogDismissListener {
    private SlidingTabLayout tabMenu;
    private ViewPager vpDetail;
    private Toolbar toolbar;
    private FragmentManager fragmentManager;
    private String[] titles = new String[]{"采集文件管理", "错误日志管理"};
    private Button exportRecord;

    private FileManagerContract.FileManagerPresenter presenter;
    private WarningDialog warningDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file_manager);
        presenter = new FileManagerPresenter(this, this);
        initData();
        initFragment();
        initVie();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    private void initData() {

    }

    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
    }

    private void initVie() {
        toolbar = (Toolbar) findViewById(R.id.file_tl_bar);
        setSupportActionBar(toolbar);
        exportRecord = (Button) findViewById(R.id.export_record);
        exportRecord.setOnClickListener(this);
//        getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        vpDetail = (ViewPager) findViewById(R.id.file_pager_info);
        vpDetail.setOffscreenPageLimit(2);
        vpDetail.setAdapter(new FileManagerAdapter(fragmentManager, titles));

        tabMenu = (SlidingTabLayout) findViewById(R.id.file_tab_menu);
        tabMenu.setSelectedIndicatorColors(ContextCompat.getColor(FileManagerActivity.this, R.color.indicator_color));
        tabMenu.setCustomTabView(R.layout.tab_indicator, android.R.id.text1);

        tabMenu.setDistributeEvenly(true);
        tabMenu.setViewPager(vpDetail);
        tabMenu.setOnPageChangeListener(this);
        vpDetail.setCurrentItem(0);
        initDialog();


    }

    private void initDialog() {
        warningDialog = new WarningDialog(this);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

    }

    @Override
    public void initView() {

    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startActivity() {

    }

    @Override
    public void printLog(String msg) {

    }


    @Override
    protected void onResume() {
        super.onResume();
        UMengUtil.getInstance().onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (warningDialog.isShowing()) {
            warningDialog.dismiss();
        }
        UMengUtil.getInstance().onPause(this);
    }

    public void collectFileManager(View view) {

        Intent intent = new Intent(this, FileViewActivity.class);
        intent.putExtra("view", "collect");
        startActivity(intent);
    }

    public void errorFileManager(View view) {

        Intent intent = new Intent(this, FileViewActivity.class);
        intent.putExtra("view", "error");
        startActivity(intent);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.export_record:
                warningDialog.show();
                warningDialog.setErrorTitle("警告");
                warningDialog.setErrorMsg("导出后，数据将被清空，请确认");
                warningDialog.setBtnTxt("确认");
                warningDialog.setVisibilityErrorCode(View.GONE);
                warningDialog.setOnDialogDismissListener(this);
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (warningDialog.isShowing()) {
            warningDialog.dismiss();
        }
    }

    @Override
    public void dismiss() {
        presenter.exportAllMallsSignal(presenter.getMalls());
    }
}
